package com.example.feiyu.driveronline.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.feiyu.driveronline.R;
import com.example.feiyu.driveronline.domain.RankingItem;

import java.util.List;

/**
 * Created by feiyu on 17-3-31.
 */

public class RankingAdapter extends RecyclerView.Adapter<RankingAdapter.ViewHolder> {

    private List<RankingItem> ranklist;

    public RankingAdapter(List<RankingItem> ranklist) {
        this.ranklist = ranklist;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.ranking_item_layout, parent, false);

        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        RankingItem rankitem = ranklist.get(position);
        holder.driverName_tv.setText(rankitem.getDriverName());
        holder.driverScore_tv.setText(rankitem.getDriverScore()+"");
        switch (rankitem.getRankNum()){
            case 1:
                holder.rankNum_iv.setVisibility(View.VISIBLE);
                holder.rankNum_iv.setImageResource(R.drawable.medal_gold);
                holder.rankNum_tv.setVisibility(View.GONE);
                break;
            case 2:
                holder.rankNum_iv.setVisibility(View.VISIBLE);
                holder.rankNum_iv.setImageResource(R.drawable.medal_silver);
                holder.rankNum_tv.setVisibility(View.GONE);
                break;
            case 3:
                holder.rankNum_iv.setVisibility(View.VISIBLE);
                holder.rankNum_iv.setImageResource(R.drawable.medal_bronze);
                holder.rankNum_tv.setVisibility(View.GONE);
                break;
            default:
                holder.rankNum_iv.setVisibility(View.GONE);
                holder.rankNum_tv.setVisibility(View.VISIBLE);
                holder.rankNum_tv.setText(rankitem.getRankNum()+"");
                break;
        }
    }

    @Override
    public int getItemCount() {
        return ranklist.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        ImageView rankNum_iv;
        TextView rankNum_tv;
        TextView driverName_tv;
        TextView driverScore_tv;

        public ViewHolder(View itemView) {
            super(itemView);
            rankNum_iv = (ImageView) itemView.findViewById(R.id.ranking_num_iv);
            rankNum_tv = (TextView) itemView.findViewById(R.id.ranking_num_tv);
            driverName_tv = (TextView) itemView.findViewById(R.id.driver_name_tv);
            driverScore_tv = (TextView) itemView.findViewById(R.id.driver_score_tv);
        }
    }
}
